package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.passport.dao.theme.DownloadHistoryDao;
import com.theme.passport.entity.theme.DownloadHistoryEntity;
import com.theme.passport.service.theme.DownloadHistoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("downloadHistoryService")
public class DownloadHistoryServiceImpl extends ServiceImpl<DownloadHistoryDao, DownloadHistoryEntity> implements DownloadHistoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String userId = (String) params.get("userId");
        String type = (String) params.get("type");
        String objectId = (String) params.get("objectId");
        IPage<DownloadHistoryEntity> page = this.page(
                new Query<DownloadHistoryEntity>().getPage(params),
                new QueryWrapper<DownloadHistoryEntity>()
                        .eq(StringUtils.isNotBlank(userId), "user_id", userId)
                        .eq(StringUtils.isNotBlank(type), "type", type)
                        .eq(StringUtils.isNotBlank(objectId), "object_id", objectId)
                        .orderByDesc("created")
        );
        return new PageUtils(page);
    }

    @Override
    public List<Long> queryHotDownloadIds(Map<String, Object> params) {
        List<Map<String, Object>> downloadHot = baseMapper.queryDownloadHot(params);
        List<Long> ids = new ArrayList<>();
        if (downloadHot.size() > 0) {
            for (Map<String, Object> m : downloadHot) {
                Long themeId = (Long) m.get("objectId");
                ids.add(themeId);
            }
            return ids;
        }
        return null;
    }

}